package com.demo.core.dao;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.demo.base.model.SystemLog;

@Repository
public class SystemLogDao extends BaseDao{

	/**
	 * 分页查询系统日志
	 * @param systemLog
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SystemLog> listPagedSystemLog(SystemLog systemLog){
		List<Object> args = new ArrayList<Object>();
		StringBuffer sql = new StringBuffer();
		sql.append("\n from SystemLog");
		sql.append("\n where 1=1 ");
		
		//根据操作者姓名模糊查询
		if(StringUtils.isNotEmpty(systemLog.getUserName())){
			this.addSqlWhereLike(systemLog.getUserName(), sql, args, " and userName like ? ");
		}
		
		Session session = this.getSession();
		List<SystemLog> list = session.createQuery(sql.toString()).list();
		
		return list;		
	}

	/**
	 * 获取超出10w的日志
	 * @param num
	 * @return
	 */
	public void delMoreSystemLog() {
		
		String sql = "delete from systemLog where id in(select id from systemLog order by recordDateTime limit 100000,n)";
		
		Session session = this.getSession();
		session.createQuery(sql).executeUpdate();
	}

}
